//
//  MoreTableViewCell.m
//  微blog
//
//  Created by Jorn.Wu on 15/8/21.
//  Copyright (c) 2015年 Jorn.Wu(jorn_wza@sina.com). All rights reserved.
//

#import "MoreTableViewCell.h"
#import "ThemeManager.h"
#import "MoreTableViewCellModel.h"

@implementation MoreTableViewCell{
    ThemeManager *_manager;
}

/**
 * 重写dealloc方法，旨在移除观察者
 */
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeDidChangedNotification object:nil];
}


/**
 * 重写initWithCoder方法，旨在让cell可以响应切换主题通知
 */
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        _manager = [ThemeManager defualtManager];
        
        /**
         * 注册成为接收者
         */
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    }
    
    return self;
    
}


/**
 * 重写awakeFromNib方法，旨在方便XIB创建
 */
- (void)awakeFromNib{
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChangedNotificationAction:) name:kThemeDidChangedNotification object:nil];
    
}


/**
 * 通知的响应方法
 */
- (void)themeDidChangedNotificationAction:(NSNotification *)noti{
    [self reloadIamge];
    [self reloadTextColor];
    [self reloadBackgroundColor];
}


/**
 * 重写model的set方法，更改model
 */
- (void)setModel:(MoreTableViewCellModel *)model{
    
    _model = model;
    
    self.textLabel.text = [model.text copy];
    self.detailTextLabel.text = [model.detailText copy];
    self.themeIconName = [model.iconName copy];
    self.themeTextColorName = [model.textcolorName copy];
    self.themeBackgroudColorName = [model.backgroudColorName copy];
   
    
}


//--------------------------------------设置Icon图片-----------------------------------------------------

/**
 * 重新加载图片
 */
- (void)reloadIamge{
    
    /**
     * 通过主题管理对象manager来获取图
     */
    UIImage *image = [_manager getThemeImage:self.themeIconName];
    
    /**
     * 设置icon图片
     */
    if (image) {
        self.imageView.image = image;
    }
}


/**
 * 重新set方法，旨在更改图片
 */
- (void)setThemeIconName:(NSString *)themeIconName{
    
    if (![_themeIconName isEqualToString:themeIconName]) {
        _themeIconName = [themeIconName copy];

        [self reloadIamge];
    }
    
}

//--------------------------------------设置text颜色-----------------------------------------------------

/**
 * 重新加载text颜色
 */
- (void)reloadTextColor{
    
    /**
     * 通过主题管理对象manager来获取文字颜色
     */
    UIColor *textColor = [_manager getThemeColor:self.themeTextColorName];
    
    /**
     * 设置颜色
     */
    if (textColor) {
        
        self.textLabel.textColor = textColor;
        self.detailTextLabel.textColor = textColor;
        
    }
    
}


/**
 * 重写set方法，旨在更改text颜色
 */
- (void)setThemeTextColorName:(NSString *)themeTextColorName{
    if (![_themeTextColorName isEqualToString:themeTextColorName]) {
        _themeTextColorName = [themeTextColorName copy];

        [self reloadTextColor];
    }
}

//--------------------------------------设置cell背景颜色-----------------------------------------------------

/**
 * 重新加载背景颜色
 */
- (void)reloadBackgroundColor{
    
    /**
     * 通过主题管理对象manager来获取背景颜色
     */
    UIColor *backgroudColor = [_manager getThemeColor:self.themeBackgroudColorName];
    
    /**
     * 设置颜色
     */
    if (backgroudColor) {
        self.backgroundColor = backgroudColor;
    }
    
}


/**
 * 重写set方法，旨在更改背景颜色
 */
- (void)setThemeBackgroudColorName:(NSString *)themeBackgroudColorName{
    if (![_themeBackgroudColorName isEqualToString:themeBackgroudColorName]) {
        _themeBackgroudColorName = [themeBackgroudColorName copy];
        [self reloadBackgroundColor];
        
    }
}


//-------------------------------------------------------------------------------------------













- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
